package com.topisv.tms.service.stock.instorage;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.topisv.tms.entity.EdiGoodsSku;
import com.topisv.tms.entity.dto.InstorageInfoDto;
import com.topisv.tms.entity.stock.instorage.InstorageInfo;
import com.topisv.tms.repository.EdiGoodsSkuDao;
import com.topisv.tms.repository.stock.instorage.InstorageInfoDao;

/**
 * 入库历史记录_service
 * @author ch
 *
 */
@Component
@Transactional(readOnly = true)
public class InstorageInfoService {
	
	@Autowired
	private InstorageInfoDao instorageInfoDao;
	@Autowired
	private EdiGoodsSkuDao ediGoodsSkuDao;

	
	public List<InstorageInfoDto> loadInfoDtoByParam(Map<String, Object> param) {
		
		 String customerCode=(String) param.get("customerCode");
		 String warehouseCode=(String) param.get("warehouseCode");
		
		Map<String, Integer> map_customer=new HashMap<String, Integer>();
		Map<String, Integer> map_allot=new HashMap<String, Integer>();
		Map<String, Integer> map_returnOrder=new HashMap<String, Integer>();
		Map<String, Integer> map_total=new HashMap<String, Integer>();
		
		List<InstorageInfoDto> dtoList=new ArrayList<InstorageInfoDto>();
		List<InstorageInfo> infoList=this.loadByParam(param);
		
		for (InstorageInfo info : infoList) {
			
			String sku=info.getSku();
			int num=info.getNum();
			
			//入库单类型 (customer. 采购入库 allot.调拨入库 returnOrder：退回入库 preOrder: 采购预约入库)
			String billType=info.getBillType();
			
			if("customer".equals(billType) || "preOrder".equals(billType) ){ //采购入库
				
				if(map_customer.keySet().contains(sku)){
					map_customer.put(sku,map_customer.get(sku)+num);
				}else{
					map_customer.put(sku, num);
				}
				
			}else if("allot".equals(billType)){ //调拨入库
				
				if(map_allot.keySet().contains(sku)){
					map_allot.put(sku,map_allot.get(sku)+num);
				}else{
					map_allot.put(sku, num);
				}
				
			}else if("returnOrder".equals(billType) || "cancelOrder".equals(billType) ||  "cancel".equals(billType) ){ //退回入库
				
				if(map_returnOrder.keySet().contains(sku)){
					map_returnOrder.put(sku,map_returnOrder.get(sku)+num);
				}else{
					map_returnOrder.put(sku, num);
				}
				
			}
			
			if(map_total.keySet().contains(sku)){
				map_total.put(sku,map_total.get(sku)+num);
			}else{
				map_total.put(sku, num);
			}
			
		}
		
	    for(String sku:map_total.keySet()){
	    	
	    	InstorageInfoDto dto=new InstorageInfoDto();
	    	dto.setCustomerCode(customerCode);
	    	dto.setWarehouseCode(warehouseCode);
	    	dto.setSku(sku);
	    	EdiGoodsSku ediGoodsSku = ediGoodsSkuDao.findByCustomerCodeAndSku(customerCode, sku);
	    	dto.setSkuName(ediGoodsSku == null ? "" : ediGoodsSku.getSkuName());
	    	
	    	
	    	//合计
	    	dto.setTotalNum(map_total.get(sku));
	    	
	    	//采购入库
	    	Integer customerNum=map_customer.get(sku);
	    	dto.setCustomerNum(customerNum==null?0:customerNum);
	    	
	    	//调拨入库
	    	Integer allotNum=map_allot.get(sku);
	    	dto.setAllotNum(allotNum==null?0:allotNum);
	    	
	    	//退回入库
	    	Integer returnNum=map_returnOrder.get(sku);
	    	dto.setReturnNum(returnNum==null?0:returnNum);
	    	
	    	dtoList.add(dto);
	    }
		return dtoList;
	}
	
	
	/**
	 * 查找入库信息
	 * @param searchParams 查询参数
	 * @return
	 */
	public List<InstorageInfo> loadByParam(Map<String, Object> param) {
		Specification<InstorageInfo> spec = getWhereClause(param);
		List<InstorageInfo> infoList = this.instorageInfoDao.findAll(spec);
		return infoList;
	}
	
	/**
	 * 查询条件封装
	 * @param param
	 * @return
	 */
	private Specification<InstorageInfo> getWhereClause(final Map<String, Object> param) {
		
		return new Specification<InstorageInfo>(){
			
		    @Override
		    public Predicate toPredicate(Root<InstorageInfo> r, CriteriaQuery<?> q, CriteriaBuilder cb) {
		      Predicate pre = cb.conjunction();
		      
		      String customerCode=(String) param.get("customerCode");
		      if(StringUtils.isNotBlank(customerCode)){
					pre.getExpressions().add(cb.equal(r.get("customerCode"),customerCode.trim()));
			  }
		      
		      String warehouseCode=(String) param.get("warehouseCode");
		      if(StringUtils.isNotBlank(warehouseCode)){
					pre.getExpressions().add(cb.equal(r.get("warehouseCode"),warehouseCode.trim()));
			  }

		      String sku=(String) param.get("sku");
		      if(StringUtils.isNotBlank(sku)){
					pre.getExpressions().add(cb.equal(r.get("sku"),sku));
			  }	
		      
		      String billCode=(String) param.get("billCode");
		      if(StringUtils.isNotBlank(billCode)){
					pre.getExpressions().add(cb.equal(r.get("billCode"),billCode));
			  }	
		      
		      String billType=(String) param.get("billType");
		      if(StringUtils.isNotBlank(billType)){
		    	  pre.getExpressions().add(cb.equal(r.get("billType"),billType));
			  }	
		      
		      Date fromDate= (Date) param.get("fromDate");
		      if (fromDate != null) {
		        pre.getExpressions().add(cb.greaterThanOrEqualTo(r.<Date>get("opeTime"), fromDate));
		      }
		      
		      Date toDate= (Date) param.get("toDate");
		      if (toDate != null) {
		        pre.getExpressions().add(cb.lessThanOrEqualTo(r.<Date>get("opeTime"), toDate));
		      }
		      
		      return pre;
		    }
		  };
       }

	
}
